{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0d4a77a4",
   "metadata": {},
   "source": [
    "# Building a One-Liner Research Agent\n",
    "\n",
    "Research tasks consume hours of expert time: market analysts manually gathering competitive intelligence, legal teams tracking regulatory changes, engineers investigating bug reports across documentation. The core challenge isn't finding information but knowing what to search for next based on what you just discovered.\n",
    "\n",
    "The Claude Agent SDK makes it possible to build agents that autonomously explore external systems without a predefined workflow. Unlike traditional workflow automations that follow fixed steps, research agents adapt their strategy based on what they find--following promising leads, synthesizing conflicting sources, and knowing when they have enough information to answer the question.\n",
    "\n",
    "## By the end of this cookbook, you'll be able to:\n",
    "\n",
    "- Build a research agent that autonomously searches and synthesizes information with a few lines of code\n",
    "\n",
    "This foundation applies to any task where the information needed isn't available upfront: competitive analysis, technical troubleshooting, investment research, or literature reviews.\n",
    "\n",
    "# Why Research Agents?\n",
    "\n",
    "Research is an ideal agentic use case for two reasons:\n",
    "\n",
    "1. **Information isn't self-contained**. The input question alone doesn't contain the answer. The agent must interact with external systems (search engines, databases, APIs) to gather what it needs.\n",
    "2. **The path emerges during exploration**. You can't predetermine the workflow. Whether an agent should search for company financials or regulatory filings depends on what it discovers about the business model. The optimal strategy reveals itself through investigation.\n",
    "\n",
    "In its simplest form, a research agent searches the web and synthesizes findings. Below, we'll build exactly that with the Claude Agent SDK's built-in web search tool in just a few lines of code.\n",
    "\n",
    "Note: You can also view the full list of [Claude Code's built-in tools](https://docs.claude.com/en/docs/claude-code/settings#tools-available-to-claude)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "301fb086",
   "metadata": {},
   "source": [
    "# Prerequisites\n",
    "\n",
    "Before following this guide, ensure you have:\n",
    "\n",
    "**Required Knowledge**\n",
    "\n",
    "* Python fundamentals - comfortable with async/await, functions, and basic data structures\n",
    "* Basic understanding of agentic patterns - we recommend reading [Building effective agents](https://www.anthropic.com/engineering/building-effective-agents) first if you're new to agents\n",
    "\n",
    "**Required Tools**\n",
    "\n",
    "* Python 3.11 or higher\n",
    "* Anthropic API key [(get one here)](https://console.anthropic.com)\n",
    "\n",
    "**Recommended:**\n",
    "* Familiarity with the Claude Agent SDK concepts\n",
    "* Understanding of tool use patterns in LLMs\n",
    "\n",
    "\n",
    "## Setup\n",
    "\n",
    "First, install the required dependencies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab9830f9",
   "metadata": {},
   "outputs": [],
   "source": "%%capture\n%pip install -U claude-agent-sdk python-dotenv"
  },
  {
   "cell_type": "markdown",
   "id": "d88272cf",
   "metadata": {},
   "source": [
    "Note: Ensure your .env file contains:\n",
    "\n",
    "```bash\n",
    "ANTHROPIC_API_KEY=your_key_here\n",
    "```\n",
    "\n",
    "Load your environment variables and configure the client:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c41abcdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "MODEL = \"claude-opus-4-5\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "041415b8",
   "metadata": {},
   "source": [
    "## Building Your First Research Agent\n",
    "\n",
    "Let's start with the simplest possible implementation: a research agent that can search the web and synthesize findings. With the Claude Agent SDK, this takes just a few lines of code.\n",
    "\n",
    "The key is the query() function, which creates a stateless agent interaction. We'll provide Claude with a single tool, WebSearch, and let it autonomously decide when and how to use it based on our research question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b00890fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "🤖 Using: WebSearch()\n",
      "🤖 Using: WebSearch()\n",
      "🤖 Using: WebSearch()\n",
      "✓ Tool completed\n",
      "✓ Tool completed\n",
      "✓ Tool completed\n",
      "🤖 Thinking...\n"
     ]
    }
   ],
   "source": [
    "from utils.agent_visualizer import (\n",
    "    display_agent_response,\n",
    "    print_activity,\n",
    ")\n",
    "\n",
    "from claude_agent_sdk import ClaudeAgentOptions, query\n",
    "\n",
    "messages = []\n",
    "async for msg in query(\n",
    "    prompt=\"Research the latest trends in AI agents and give me a brief summary and relevant citiations links.\",\n",
    "    options=ClaudeAgentOptions(model=MODEL, allowed_tools=[\"WebSearch\"]),\n",
    "):\n",
    "    print_activity(msg)\n",
    "    messages.append(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e4556936",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<style>\n",
       ".pretty-card {\n",
       "    font-family: ui-sans-serif, system-ui;\n",
       "    border: 2px solid transparent;\n",
       "    border-radius: 14px;\n",
       "    padding: 14px 16px;\n",
       "    margin: 10px 0;\n",
       "    background: linear-gradient(#fff, #fff) padding-box,\n",
       "                linear-gradient(135deg, #3b82f6, #9333ea) border-box;\n",
       "    color: #111;\n",
       "    box-shadow: 0 4px 12px rgba(0,0,0,.08);\n",
       "}\n",
       ".pretty-title {\n",
       "    font-weight: 700;\n",
       "    margin-bottom: 8px;\n",
       "    font-size: 14px;\n",
       "    color: #111;\n",
       "}\n",
       ".pretty-card pre,\n",
       ".pretty-card code {\n",
       "    background: #f3f4f6;\n",
       "    color: #111;\n",
       "    padding: 8px;\n",
       "    border-radius: 8px;\n",
       "    display: block;\n",
       "    overflow-x: auto;\n",
       "    font-size: 13px;\n",
       "    white-space: pre-wrap;\n",
       "}\n",
       ".pretty-card img {\n",
       "    max-width: 100%;\n",
       "    height: auto;\n",
       "    border-radius: 8px;\n",
       "}\n",
       "/* Tables: both pandas (.pretty-table) and markdown-rendered */\n",
       ".pretty-card table {\n",
       "    border-collapse: collapse;\n",
       "    width: 100%;\n",
       "    font-size: 13px;\n",
       "    color: #111;\n",
       "    margin: 0.5em 0;\n",
       "}\n",
       ".pretty-card th,\n",
       ".pretty-card td {\n",
       "    border: 1px solid #e5e7eb;\n",
       "    padding: 6px 8px;\n",
       "    text-align: left;\n",
       "}\n",
       ".pretty-card th {\n",
       "    background: #f9fafb;\n",
       "    font-weight: 600;\n",
       "}\n",
       "/* Markdown headings */\n",
       ".pretty-card h1, .pretty-card h2, .pretty-card h3, .pretty-card h4 {\n",
       "    margin: 0.5em 0 0.3em 0;\n",
       "    color: #111;\n",
       "}\n",
       ".pretty-card h1 { font-size: 1.4em; }\n",
       ".pretty-card h2 { font-size: 1.2em; }\n",
       ".pretty-card h3 { font-size: 1.1em; }\n",
       "/* Markdown lists and paragraphs */\n",
       ".pretty-card ul, .pretty-card ol {\n",
       "    margin: 0.5em 0;\n",
       "    padding-left: 1.5em;\n",
       "}\n",
       ".pretty-card p {\n",
       "    margin: 0.5em 0;\n",
       "}\n",
       ".pretty-card hr {\n",
       "    border: none;\n",
       "    border-top: 1px solid #e5e7eb;\n",
       "    margin: 1em 0;\n",
       "}\n",
       "</style>\n",
       "<div class=\"pretty-card\"><div class=\"pretty-title\">Agent Response</div><h2>Latest Trends in AI Agents (2025) - Summary</h2>\n",
       "<h3>🚀 Market Growth &amp; Adoption</h3>\n",
       "<p>The AI agent market is experiencing explosive growth, nearly doubling from <strong>$3.7 billion (2023) to $7.38 billion (2025)</strong>, with projections reaching <strong>$103.6 billion by 2032</strong>. According to PwC's 2025 survey, <strong>79% of organizations</strong> have adopted AI agents, with <strong>88% of executives</strong> piloting or scaling autonomous agent systems.</p>\n",
       "<h3>🔑 Key Trends</h3>\n",
       "<p><strong>1. Rise of Multi-Agent Systems</strong><br />\n",
       "Instead of single AI systems trying to do everything, 2025 has introduced the \"orchestra approach\" where multiple specialized agents collaborate—one gathers research, another drafts reports, and a third reviews. Frameworks like <strong>CrewAI, AutoGen, and LangGraph</strong> are enabling this coordination across enterprise departments.</p>\n",
       "<p><strong>2. From Assistants to Autonomous Decision-Makers</strong><br />\n",
       "AI agents are evolving from knowledge assistants to <strong>self-directed workers</strong> that can take initiative, make decisions, and complete multi-step tasks without constant human input. By 2029, <strong>80% of customer service issues</strong> are expected to be resolved entirely by autonomous agents.</p>\n",
       "<p><strong>3. Model Context Protocol (MCP)</strong><br />\n",
       "Anthropic's open standard provides a \"USB-C for AI\"—standardizing how language models connect with external systems, enabling structured multi-step workflows and access to real-time information.</p>\n",
       "<p><strong>4. Two-Speed Enterprise Landscape</strong><br />\n",
       "A divide is emerging: companies with existing automation are racing ahead with agentic AI, while others watch from the sidelines. Among highly automated enterprises, <strong>50%</strong> have either adopted or are preparing to adopt autonomous agents.</p>\n",
       "<h3>⚠️ Key Challenges</h3>\n",
       "<ul>\n",
       "<li><strong>Integration with legacy systems</strong> (cited by ~60% of AI leaders)</li>\n",
       "<li><strong>Trust issues</strong> for high-stakes tasks like financial transactions</li>\n",
       "<li><strong>Enterprise readiness</strong>—organizations need to expose APIs and prepare infrastructure</li>\n",
       "<li><strong>Reliability concerns</strong>—agents can misinterpret instructions or fail on edge cases</li>\n",
       "</ul>\n",
       "<h3>💼 Impact</h3>\n",
       "<ul>\n",
       "<li><strong>66%</strong> of adopters report measurable productivity gains</li>\n",
       "<li>Early movers are cutting operational costs by up to <strong>40%</strong></li>\n",
       "<li><strong>75% of executives</strong> believe AI agents will reshape the workplace more than the internet did</li>\n",
       "<li><strong>87%</strong> agree AI agents augment roles rather than replace them</li>\n",
       "</ul>\n",
       "<hr />\n",
       "<h2>Sources:</h2>\n",
       "<ul>\n",
       "<li><a href=\"https://www.mckinsey.com/capabilities/quantumblack/our-insights/the-state-of-ai\">The State of AI in 2025 - McKinsey</a></li>\n",
       "<li><a href=\"https://www.ibm.com/think/insights/ai-agents-2025-expectations-vs-reality\">AI Agents in 2025: Expectations vs. Reality - IBM</a></li>\n",
       "<li><a href=\"https://www.pwc.com/us/en/tech-effect/ai-analytics/ai-agent-survey.html\">PwC's AI Agent Survey</a></li>\n",
       "<li><a href=\"https://www.mckinsey.com/capabilities/quantumblack/our-insights/seizing-the-agentic-ai-advantage\">Seizing the Agentic AI Advantage - McKinsey</a></li>\n",
       "<li><a href=\"https://www.gartner.com/en/newsroom/press-releases/2025-08-05-gartner-hype-cycle-identifies-top-ai-innovations-in-2025\">Gartner Hype Cycle Identifies Top AI Innovations in 2025</a></li>\n",
       "<li><a href=\"https://www.deloitte.com/us/en/services/consulting/blogs/ai-adoption-challenges-ai-trends.html\">AI Trends 2025: Adoption Barriers - Deloitte</a></li>\n",
       "<li><a href=\"https://aws.amazon.com/blogs/aws-insights/the-rise-of-autonomous-agents-what-enterprise-leaders-need-to-know-about-the-next-wave-of-ai/\">The Rise of Autonomous Agents - AWS</a></li>\n",
       "<li><a href=\"https://terralogic.com/multi-agent-ai-systems-why-they-matter-2025/\">Multi-Agent AI Systems in 2025 - Terralogic</a></li>\n",
       "<li><a href=\"https://www.index.dev/blog/ai-agents-statistics\">50+ Key AI Agent Statistics - Index.dev</a></li>\n",
       "<li><a href=\"https://www.salesforce.com/news/stories/future-of-ai-agents-2025/\">Future of AI Agents 2025 - Salesforce</a></li>\n",
       "<li><a href=\"https://superagi.com/top-5-agentic-ai-trends-in-2025-from-multi-agent-collaboration-to-self-healing-systems/\">Top 5 Agentic AI Trends - SuperAGI</a></li>\n",
       "</ul></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_agent_response(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b965c2ee",
   "metadata": {},
   "source": [
    "## What's happening here:\n",
    "\n",
    "- `query()` creates a single-turn agent interaction (no conversation memory)\n",
    "- `allowed_tools=[\"WebSearch\"]` gives Claude permission to search the web without asking for approval\n",
    "- The agent autonomously decides when to search, what queries to run, and how to synthesize results\n",
    "\n",
    "**Visualization utilities from `utils.agent_visualizer`:**\n",
    "- `print_activity()` - Shows the agent's actions in real-time (tool calls, thinking)\n",
    "- `display_agent_response()` - Renders the final response as a styled HTML card\n",
    "- `visualize_conversation()` - Creates a timeline view of the full conversation\n",
    "\n",
    "That's it! A functional research agent in just a few lines of code. The agent will search for relevant information, follow up on promising leads, and provide a synthesized summary with citations."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b888772",
   "metadata": {},
   "source": [
    "The query() function creates a stateless agent interaction. Each call is independent—no conversation memory, no context from previous queries. This makes it perfect for one-off research tasks where you need a quick answer without maintaining state.\n",
    "\n",
    "**How tool permissions work:**\n",
    "\n",
    "The `allowed_tools=[\"WebSearch\"]` parameter gives Claude permission to search without asking for approval. This is critical for autonomous operation:\n",
    "\n",
    "- `Allowed tools` - Claude can use these freely (in this case, WebSearch)\n",
    "- `Other tools` - Available but require approval before use\n",
    "- `Read-only tools` - Tools like Read are always allowed by default\n",
    "- `Disallowed tools` - Add tools to disallowed_tools to remove them entirely from Claude's context\n",
    "\n",
    "**When to use stateless queries:**\n",
    "\n",
    "- One-off research questions where context doesn't matter\n",
    "- Parallel processing of independent research tasks\n",
    "- Scenarios where you want fresh context for each query\n",
    "\n",
    "**When not to use stateless queries:**\n",
    "\n",
    "- Multi-turn investigations that build on previous findings\n",
    "- Iterative refinement of research based on initial results\n",
    "- Complex analysis requiring sustained context\n",
    "\n",
    "Let's inspect what the agent actually did using the visualize_conversation helper:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1d7c6d90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    \n",
       "<style>\n",
       ".conversation-timeline {\n",
       "    font-family: ui-sans-serif, system-ui;\n",
       "    max-width: 900px;\n",
       "    margin: 1em 0;\n",
       "}\n",
       ".timeline-header {\n",
       "    background: linear-gradient(135deg, #3b82f6, #9333ea);\n",
       "    color: white;\n",
       "    padding: 12px 16px;\n",
       "    border-radius: 12px 12px 0 0;\n",
       "    font-weight: 700;\n",
       "    font-size: 14px;\n",
       "}\n",
       ".timeline-body {\n",
       "    border: 1px solid #e5e7eb;\n",
       "    border-top: none;\n",
       "    border-radius: 0 0 12px 12px;\n",
       "    padding: 12px;\n",
       "    background: #fafafa;\n",
       "}\n",
       ".msg-block {\n",
       "    margin: 8px 0;\n",
       "    padding: 10px 12px;\n",
       "    border-radius: 8px;\n",
       "    background: white;\n",
       "    border-left: 3px solid #e5e7eb;\n",
       "}\n",
       ".msg-block.system { border-left-color: #6b7280; }\n",
       ".msg-block.assistant { border-left-color: #3b82f6; }\n",
       ".msg-block.tool { border-left-color: #10b981; background: #f0fdf4; }\n",
       ".msg-block.subagent { border-left-color: #9333ea; background: #faf5ff; }\n",
       ".msg-block.result { border-left-color: #f59e0b; background: #fffbeb; }\n",
       ".msg-label {\n",
       "    font-size: 11px;\n",
       "    font-weight: 600;\n",
       "    text-transform: uppercase;\n",
       "    color: #6b7280;\n",
       "    margin-bottom: 4px;\n",
       "}\n",
       ".msg-content {\n",
       "    font-size: 13px;\n",
       "    color: #111;\n",
       "}\n",
       ".msg-content pre {\n",
       "    background: #f3f4f6;\n",
       "    padding: 8px;\n",
       "    border-radius: 6px;\n",
       "    overflow-x: auto;\n",
       "    font-size: 12px;\n",
       "}\n",
       ".tool-list {\n",
       "    display: flex;\n",
       "    flex-wrap: wrap;\n",
       "    gap: 6px;\n",
       "    margin-top: 4px;\n",
       "}\n",
       ".tool-badge {\n",
       "    background: #e0f2fe;\n",
       "    color: #0369a1;\n",
       "    padding: 2px 8px;\n",
       "    border-radius: 4px;\n",
       "    font-size: 11px;\n",
       "    font-family: monospace;\n",
       "}\n",
       ".stats-bar {\n",
       "    display: flex;\n",
       "    gap: 16px;\n",
       "    padding: 10px 12px;\n",
       "    background: #f9fafb;\n",
       "    border-radius: 8px;\n",
       "    font-size: 12px;\n",
       "    color: #374151;\n",
       "    margin-top: 8px;\n",
       "}\n",
       ".stat-item { display: flex; gap: 4px; }\n",
       ".stat-label { color: #6b7280; }\n",
       "</style>\n",
       "\n",
       "    <div class=\"conversation-timeline\">\n",
       "        <div class=\"timeline-header\">🤖 Agent Conversation Timeline • claude-opus-4-5</div>\n",
       "        <div class=\"timeline-body\">\n",
       "            <div class=\"msg-block system\"><div class=\"msg-label\">⚙️ System</div><div class=\"msg-content\">Initialized (4e8497a9...)</div></div><div class=\"msg-block tool\"><div class=\"msg-label\">🔧 Tools</div><div class=\"tool-list\"><span class=\"tool-badge\">WebSearch: &quot;AI agents trends 2025 latest d...&quot;</span><span class=\"tool-badge\">WebSearch: &quot;autonomous AI agents enterpris...&quot;</span><span class=\"tool-badge\">WebSearch: &quot;multi-agent AI systems trends ...&quot;</span></div></div><div class=\"msg-block assistant\"><div class=\"msg-label\">🤖 Assistant</div><div class=\"msg-content\"><h2>Latest Trends in AI Agents (2025) - Summary</h2>\n",
       "<h3>🚀 Market Growth &amp; Adoption</h3>\n",
       "<p>The AI agent market is experiencing explosive growth, nearly doubling from <strong>$3.7 billion (2023) to $7.38 billion (2025)</strong>, with projections reaching <strong>$103.6 billion by 2032</strong>. According to PwC's 2025 survey, <strong>79% of organizations</strong> have adopted AI agents, with <strong>88% of executives</strong> piloting or scaling autonomous agent systems.</p>\n",
       "<h3>🔑 Key Trends</h3>\n",
       "<p><strong>1. Rise of Multi-Agent Systems</strong><br />\n",
       "Instead of single AI systems trying to do everything, 2025 has introduced the \"orchestra approach\" where multiple specialized agents collaborate—one gathers research, another drafts reports, and a third reviews. Frameworks like <strong>CrewAI, AutoGen, and LangGraph</strong> are enabling this coordination across enterprise departments.</p>\n",
       "<p><strong>2. From Assistants to Autonomous Decision-Makers</strong><br />\n",
       "AI agents are evolving from knowledge assistants to <strong>self-directed workers</strong> that can take initiative, make decisions, and complete multi-step tasks without constant human input. By 2029, <strong>80% of customer service issues</strong> are expected to be resolved entirely by autonomous agents.</p>\n",
       "<p><strong>3. Model Context Protocol (MCP)</strong><br />\n",
       "Anthropic's open standard provides a \"USB-C for AI\"—standardizing how language models connect with external systems, enabling structured multi-step workflows and access to real-time information.</p>\n",
       "<p><strong>4. Two-Speed Enterprise Landscape</strong><br />\n",
       "A divide is emerging: companies with existing automation are racing ahead with agentic AI, while others watch from the sidelines. Among highly automated enterprises, <strong>50%</strong> have either adopted or are preparing to adopt autonomous agents.</p>\n",
       "<h3>⚠️ Key Challenges</h3>\n",
       "<ul>\n",
       "<li><strong>Integration with legacy systems</strong> (cited by ~60% of AI leaders)</li>\n",
       "<li><strong>Trust issues</strong> for high-stakes tasks like financial transactions</li>\n",
       "<li><strong>Enterprise readiness</strong>—organizations need to expose APIs and prepare infrastructure</li>\n",
       "<li><strong>Reliability concerns</strong>—agents can misinterpret instructions or fail on edge cases</li>\n",
       "</ul>\n",
       "<h3>💼 Impact</h3>\n",
       "<ul>\n",
       "<li><strong>66%</strong> of adopters report measurable productivity gains</li>\n",
       "<li>Early movers are cutting operational costs by up to <strong>40%</strong></li>\n",
       "<li><strong>75% of executives</strong> believe AI agents will reshape the workplace more than the internet did</li>\n",
       "<li><strong>87%</strong> agree AI agents augment roles rather than replace them</li>\n",
       "</ul>\n",
       "<hr />\n",
       "<h2>Sources:</h2>\n",
       "<ul>\n",
       "<li><a href=\"https://www.mckinsey.com/capabilities/quantumblack/our-insights/the-state-of-ai\">The State of AI in 2025 - McKinsey</a></li>\n",
       "<li><a href=\"https://www.ibm.com/think/insights/ai-agents-2025-expectations-vs-reality\">AI Agents in 2025: Expectations vs. Reality - IBM</a></li>\n",
       "<li><a href=\"https://www.pwc.com/us/en/tech-effect/ai-analytics/ai-agent-survey.html\">PwC's AI Agent Survey</a></li>\n",
       "<li><a href=\"https://www.mckinsey.com/capabilities/quantumblack/our-insights/seizing-the-agentic-ai-advantage\">Seizing the Agentic AI Advantage - McKinsey</a></li>\n",
       "<li><a href=\"https://www.gartner.com/en/newsroom/press-releases/2025-08-05-gartner-hype-cycle-identifies-top-ai-innovations-in-2025\">Gartner Hype Cycle Identifies Top AI Innovations in 2025</a></li>\n",
       "<li><a href=\"https://www.deloitte.com/us/en/services/consulting/blogs/ai-adoption-challenges-ai-trends.html\">AI Trends 2025: Adoption Barriers - Deloitte</a></li>\n",
       "<li><a href=\"https://aws.amazon.com/blogs/aws-insights/the-rise-of-autonomous-agents-what-enterprise-leaders-need-to-know-about-the-next-wave-of-ai/\">The Rise of Autonomous Agents - AWS</a></li>\n",
       "<li><a href=\"https://terralogic.com/multi-agent-ai-systems-why-they-matter-2025/\">Multi-Agent AI Systems in 2025 - Terralogic</a></li>\n",
       "<li><a href=\"https://www.index.dev/blog/ai-agents-statistics\">50+ Key AI Agent Statistics - Index.dev</a></li>\n",
       "<li><a href=\"https://www.salesforce.com/news/stories/future-of-ai-agents-2025/\">Future of AI Agents 2025 - Salesforce</a></li>\n",
       "<li><a href=\"https://superagi.com/top-5-agentic-ai-trends-in-2025-from-multi-agent-collaboration-to-self-healing-systems/\">Top 5 Agentic AI Trends - SuperAGI</a></li>\n",
       "</ul></div></div><div class=\"msg-block result\"><div class=\"msg-label\">✅ Complete</div><div class=\"stats-bar\"><span class=\"stat-item\"><span class=\"stat-label\">Turns:</span> 4</span> <span class=\"stat-item\"><span class=\"stat-label\">Tokens:</span> 4,145</span> <span class=\"stat-item\"><span class=\"stat-label\">Cost:</span> $0.51</span> <span class=\"stat-item\"><span class=\"stat-label\">Duration:</span> 51.1s</span></div></div>\n",
       "        </div>\n",
       "    </div>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from utils.agent_visualizer import visualize_conversation\n",
    "\n",
    "visualize_conversation(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22426729",
   "metadata": {},
   "source": [
    "## From Prototype to Production: Three Key Improvements\n",
    "\n",
    "Our one-line research agent works, but it's limited. Single queries without memory can't handle iterative research (\"find X, then analyze Y based on what you found\"). Let's explore three ways we can further improve our implementation.\n",
    "\n",
    "**1. Conversation Memory with ClaudeSDKClient**: Stateless queries can't build on previous findings. If you ask \"What are the top AI startups?\" then \"How are they funded?\", the second query has no context about which startups you mean. We can use `ClaudeSDKClient` to maintain conversation history across multiple queries.\n",
    "\n",
    "\n",
    "**2. System Prompts for Specialized Behavior**: Research domains often have specific requirements. Financial analysis needs different rigor than tech news summaries. Use the system prompt to encode your research standards, preferred sources, citation format, or output structure. See our [agent prompting guide](https://github.com/anthropics/anthropic-cookbook/tree/main/patterns/agents/prompts) for research-specific examples.\n",
    "\n",
    "**3. Multimodal Research with the Read Tool**: Real research isn't just text. Market reports have charts, technical docs have diagrams, competitive analysis requires screenshot comparison. Enable the `Read` tool so Claude can analyze images, PDFs, and other visual content.\n",
    "\n",
    "Let's implement these three changes for our research agent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fa4c4d8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "🤖 Using: Read()\n",
      "✓ Tool completed\n",
      "🤖 Thinking...\n",
      "🤖 Using: Glob()\n",
      "✓ Tool completed\n",
      "🤖 Thinking...\n",
      "🤖 Using: Read()\n",
      "✓ Tool completed\n",
      "🤖 Thinking...\n",
      "🤖 Using: WebSearch()\n",
      "🤖 Using: WebSearch()\n",
      "🤖 Using: WebSearch()\n",
      "🤖 Using: WebSearch()\n",
      "✓ Tool completed\n",
      "✓ Tool completed\n",
      "✓ Tool completed\n",
      "✓ Tool completed\n",
      "🤖 Thinking...\n"
     ]
    }
   ],
   "source": [
    "from claude_agent_sdk import ClaudeSDKClient\n",
    "\n",
    "# System prompt with citation requirements for research quality\n",
    "RESEARCH_SYSTEM_PROMPT = \"\"\"You are a research agent specialized in AI.\n",
    "\n",
    "When providing research findings:\n",
    "- Always include source URLs as citations\n",
    "- Format citations as markdown links: [Source Title](URL)\n",
    "- Group sources in a \"Sources:\" section at the end of your response\"\"\"\n",
    "\n",
    "messages = []\n",
    "async with ClaudeSDKClient(\n",
    "    options=ClaudeAgentOptions(\n",
    "        model=MODEL,\n",
    "        cwd=\"research_agent\",\n",
    "        system_prompt=RESEARCH_SYSTEM_PROMPT,\n",
    "        allowed_tools=[\"WebSearch\", \"Read\"],\n",
    "        max_buffer_size=10 * 1024 * 1024,  # Increase to 10MB for image handling\n",
    "    )\n",
    ") as research_agent:\n",
    "    # First query: Analyze the chart image\n",
    "    await research_agent.query(\"Analyze the chart in research_agent/projects_claude.png\")\n",
    "    async for msg in research_agent.receive_response():\n",
    "        print_activity(msg)\n",
    "        messages.append(msg)\n",
    "\n",
    "    # Second query: Use web search to validate/contextualize the chart findings\n",
    "    await research_agent.query(\n",
    "        \"Based on the chart analysis, search for recent news or data that validates or provides context for these findings. Include source URLs.\"\n",
    "    )\n",
    "    async for msg in research_agent.receive_response():\n",
    "        print_activity(msg)\n",
    "        messages.append(msg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "i4blqsetg8",
   "metadata": {},
   "source": [
    "### 🔧 Handling Large Responses and Buffer Limits\n",
    "\n",
    "When working with images or large data, you may encounter buffer overflow errors:\n",
    "\n",
    "```\n",
    "Fatal error in message reader: Failed to decode JSON: JSON message exceeded maximum buffer size of 1048576 bytes\n",
    "```\n",
    "\n",
    "**Why this happens:**\n",
    "- The default `max_buffer_size` is 1MB (1,048,576 bytes)\n",
    "- Images are base64-encoded in messages, significantly increasing size\n",
    "- The chart image (~200KB on disk) becomes ~270KB+ when base64-encoded, plus message overhead\n",
    "\n",
    "**Solution:**\n",
    "Set `max_buffer_size` in `ClaudeAgentOptions` to a higher value (e.g., 10MB) when working with images or large tool outputs.\n",
    "\n",
    "**Best practices:**\n",
    "- Set buffer size based on your use case: 10MB for typical multimodal work, higher for large document processing\n",
    "- Consider if you really need to pass full images - sometimes descriptions or smaller thumbnails suffice\n",
    "- Monitor for buffer errors and adjust accordingly\n",
    "- Include citation requirements in your system prompt to ensure verifiable research outputs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6eb4ed21",
   "metadata": {},
   "source": [
    "## What's happening here:\n",
    "\n",
    "This example combines all three improvements: conversation memory, citation-aware system prompt, and multimodal analysis.\n",
    "\n",
    "**Key components:**\n",
    "\n",
    "| Component | Purpose |\n",
    "|-----------|---------|\n",
    "| `ClaudeSDKClient` | Maintains conversation state across multiple queries |\n",
    "| `RESEARCH_SYSTEM_PROMPT` | Enforces citation formatting and source URLs |\n",
    "| `allowed_tools=[\"WebSearch\", \"Read\"]` | Enables web search and image/document analysis |\n",
    "| `max_buffer_size=10MB` | Handles base64-encoded images without overflow |\n",
    "\n",
    "**Execution flow:**\n",
    "\n",
    "1. **First query** - Analyzes the chart image using the `Read` tool\n",
    "2. **First response loop** - Collects all messages until the agent completes\n",
    "3. **Second query** - Searches the web to validate/contextualize the chart findings\n",
    "4. **Context inheritance** - The second query remembers the chart analysis from the first\n",
    "\n",
    "**Why `ClaudeSDKClient` vs `query()`:**\n",
    "\n",
    "The `async with ClaudeSDKClient()` context manager maintains conversation state. Each `receive_response()` call builds on previous context. This differs from `query()` which creates independent, stateless sessions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7971eae4-3ff1-48d8-99ef-fecca7332163",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    \n",
       "<style>\n",
       ".conversation-timeline {\n",
       "    font-family: ui-sans-serif, system-ui;\n",
       "    max-width: 900px;\n",
       "    margin: 1em 0;\n",
       "}\n",
       ".timeline-header {\n",
       "    background: linear-gradient(135deg, #3b82f6, #9333ea);\n",
       "    color: white;\n",
       "    padding: 12px 16px;\n",
       "    border-radius: 12px 12px 0 0;\n",
       "    font-weight: 700;\n",
       "    font-size: 14px;\n",
       "}\n",
       ".timeline-body {\n",
       "    border: 1px solid #e5e7eb;\n",
       "    border-top: none;\n",
       "    border-radius: 0 0 12px 12px;\n",
       "    padding: 12px;\n",
       "    background: #fafafa;\n",
       "}\n",
       ".msg-block {\n",
       "    margin: 8px 0;\n",
       "    padding: 10px 12px;\n",
       "    border-radius: 8px;\n",
       "    background: white;\n",
       "    border-left: 3px solid #e5e7eb;\n",
       "}\n",
       ".msg-block.system { border-left-color: #6b7280; }\n",
       ".msg-block.assistant { border-left-color: #3b82f6; }\n",
       ".msg-block.tool { border-left-color: #10b981; background: #f0fdf4; }\n",
       ".msg-block.subagent { border-left-color: #9333ea; background: #faf5ff; }\n",
       ".msg-block.result { border-left-color: #f59e0b; background: #fffbeb; }\n",
       ".msg-label {\n",
       "    font-size: 11px;\n",
       "    font-weight: 600;\n",
       "    text-transform: uppercase;\n",
       "    color: #6b7280;\n",
       "    margin-bottom: 4px;\n",
       "}\n",
       ".msg-content {\n",
       "    font-size: 13px;\n",
       "    color: #111;\n",
       "}\n",
       ".msg-content pre {\n",
       "    background: #f3f4f6;\n",
       "    padding: 8px;\n",
       "    border-radius: 6px;\n",
       "    overflow-x: auto;\n",
       "    font-size: 12px;\n",
       "}\n",
       ".tool-list {\n",
       "    display: flex;\n",
       "    flex-wrap: wrap;\n",
       "    gap: 6px;\n",
       "    margin-top: 4px;\n",
       "}\n",
       ".tool-badge {\n",
       "    background: #e0f2fe;\n",
       "    color: #0369a1;\n",
       "    padding: 2px 8px;\n",
       "    border-radius: 4px;\n",
       "    font-size: 11px;\n",
       "    font-family: monospace;\n",
       "}\n",
       ".stats-bar {\n",
       "    display: flex;\n",
       "    gap: 16px;\n",
       "    padding: 10px 12px;\n",
       "    background: #f9fafb;\n",
       "    border-radius: 8px;\n",
       "    font-size: 12px;\n",
       "    color: #374151;\n",
       "    margin-top: 8px;\n",
       "}\n",
       ".stat-item { display: flex; gap: 4px; }\n",
       ".stat-label { color: #6b7280; }\n",
       "</style>\n",
       "\n",
       "    <div class=\"conversation-timeline\">\n",
       "        <div class=\"timeline-header\">🤖 Agent Conversation Timeline • claude-opus-4-5</div>\n",
       "        <div class=\"timeline-body\">\n",
       "            <div class=\"msg-block system\"><div class=\"msg-label\">⚙️ System</div><div class=\"msg-content\">Initialized (9d494ab0...)</div></div><div class=\"msg-block tool\"><div class=\"msg-label\">🔧 Tools</div><div class=\"tool-list\"><span class=\"tool-badge\">Read: projects_claude.png</span></div></div><div class=\"msg-block assistant\"><div class=\"msg-label\">🤖 Assistant</div><div class=\"msg-content\"><p>Let me search for the file in different locations:</p></div></div><div class=\"msg-block tool\"><div class=\"msg-label\">🔧 Tools</div><div class=\"tool-list\"><span class=\"tool-badge\">Glob</span></div></div><div class=\"msg-block assistant\"><div class=\"msg-label\">🤖 Assistant</div><div class=\"msg-content\"><p>Found it. Let me read the image:</p></div></div><div class=\"msg-block tool\"><div class=\"msg-label\">🔧 Tools</div><div class=\"tool-list\"><span class=\"tool-badge\">Read: projects_claude.png</span></div></div><div class=\"msg-block assistant\"><div class=\"msg-label\">🤖 Assistant</div><div class=\"msg-content\"><h2>Analysis of the Chart: Types of Projects in Claude.ai and Claude Code</h2>\n",
       "<p>This is a <strong>dumbbell chart</strong> (also called a connected dot plot) that compares the prevalence of different project types between two Anthropic products: <strong>Claude.ai</strong> (blue dots) and <strong>Claude Code</strong> (orange dots).</p>\n",
       "<h3>Key Findings</h3>\n",
       "<h4>1. <strong>Personal Projects Lead Both Platforms</strong></h4>\n",
       "<ul>\n",
       "<li><strong>Claude Code</strong>: 36.0%</li>\n",
       "<li><strong>Claude.ai</strong>: 30.2%</li>\n",
       "<li>This is the most common use case for both platforms, with Claude Code having a slight edge (~6% higher).</li>\n",
       "</ul>\n",
       "<h4>2. <strong>Startup Work Shows the Largest Gap</strong></h4>\n",
       "<ul>\n",
       "<li><strong>Claude Code</strong>: 32.9%</li>\n",
       "<li><strong>Claude.ai</strong>: 13.1%</li>\n",
       "<li>This represents the <strong>biggest difference</strong> between the two platforms (~20% gap), indicating Claude Code is heavily favored for startup development work.</li>\n",
       "</ul>\n",
       "<h4>3. <strong>Enterprise Work is Relatively Balanced</strong></h4>\n",
       "<ul>\n",
       "<li><strong>Claude.ai</strong>: 25.9%</li>\n",
       "<li><strong>Claude Code</strong>: 23.8%</li>\n",
       "<li>Both platforms see similar usage for enterprise work, with Claude.ai slightly ahead (~2% difference).</li>\n",
       "</ul>\n",
       "<h4>4. <strong>Learning &amp; Academic Use Favors Claude.ai</strong></h4>\n",
       "<ul>\n",
       "<li><strong>Tutorial or Learning</strong>: Claude.ai (12.2%) vs Claude Code (0.7%)</li>\n",
       "<li><strong>Academic Research</strong>: Claude.ai (11.9%) vs Claude Code (2.2%)</li>\n",
       "<li><strong>Coursework</strong>: Claude.ai (4.9%) vs Claude Code (0.4%)</li>\n",
       "<li>These categories show Claude.ai is significantly more popular for educational purposes, likely due to its conversational interface being more accessible for learning.</li>\n",
       "</ul>\n",
       "<h4>5. <strong>Open Source Projects are Low for Both</strong></h4>\n",
       "<ul>\n",
       "<li><strong>Claude Code</strong>: 4.0%</li>\n",
       "<li><strong>Claude.ai</strong>: 1.8%</li>\n",
       "<li>Neither platform sees heavy use for open source contributions, though Claude Code is used slightly more.</li>\n",
       "</ul>\n",
       "<h3>Insights &amp; Interpretation</h3>\n",
       "<table>\n",
       "<thead>\n",
       "<tr>\n",
       "<th>Pattern</th>\n",
       "<th>Implication</th>\n",
       "</tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td><strong>Claude Code dominates Startup Work</strong></td>\n",
       "<td>Developers building products at startups prefer the code-focused interface for rapid development</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td><strong>Claude.ai leads in educational contexts</strong></td>\n",
       "<td>The conversational nature of Claude.ai makes it more approachable for learning, research, and coursework</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td><strong>Personal Projects are universal</strong></td>\n",
       "<td>Both platforms serve individual developers working on side projects equally well</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td><strong>Enterprise usage is balanced</strong></td>\n",
       "<td>Both products have found their place in professional enterprise environments</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<h3>Summary</h3>\n",
       "<p>The chart reveals clear <strong>product differentiation</strong>: <br />\n",
       "- <strong>Claude Code</strong> is preferred for <strong>active software development</strong> (startups, personal coding projects, open source)<br />\n",
       "- <strong>Claude.ai</strong> excels in <strong>knowledge work and learning</strong> (tutorials, academic research, coursework)</p>\n",
       "<p>This aligns with the nature of each product—Claude Code is a development tool while Claude.ai is a general-purpose AI assistant with a broader conversational interface.</p></div></div><div class=\"msg-block result\"><div class=\"msg-label\">✅ Complete</div><div class=\"stats-bar\"><span class=\"stat-item\"><span class=\"stat-label\">Turns:</span> 4</span> <span class=\"stat-item\"><span class=\"stat-label\">Tokens:</span> 887</span> <span class=\"stat-item\"><span class=\"stat-label\">Cost:</span> $0.07</span> <span class=\"stat-item\"><span class=\"stat-label\">Duration:</span> 27.6s</span></div></div><div class=\"msg-block system\"><div class=\"msg-label\">⚙️ System</div><div class=\"msg-content\">Initialized (9d494ab0...)</div></div><div class=\"msg-block tool\"><div class=\"msg-label\">🔧 Tools</div><div class=\"tool-list\"><span class=\"tool-badge\">WebSearch: &quot;Claude Code developer adoption...&quot;</span><span class=\"tool-badge\">WebSearch: &quot;Claude AI education academic r...&quot;</span><span class=\"tool-badge\">WebSearch: &quot;Anthropic Claude Code vs Claud...&quot;</span><span class=\"tool-badge\">WebSearch: &quot;AI coding assistants startup d...&quot;</span></div></div><div class=\"msg-block assistant\"><div class=\"msg-label\">🤖 Assistant</div><div class=\"msg-content\"><h2>Validation &amp; Context for Chart Findings</h2>\n",
       "<p>Based on my research, here's how recent news and data validate or provide context for the chart analysis:</p>\n",
       "<hr />\n",
       "<h3>1. ✅ <strong>Startup Dominance in Claude Code - VALIDATED</strong></h3>\n",
       "<p>The chart showed Claude Code usage for Startup Work at <strong>32.9%</strong> vs only <strong>13.1%</strong> for Claude.ai—the largest gap in the data. Recent news strongly confirms this:</p>\n",
       "<blockquote>\n",
       "<p><strong>\"Startups are the main early adopters of Claude Code, suggesting that smaller, agile businesses are leveraging this AI tool to gain a technological advantage.\"</strong> - <a href=\"https://blockchain.news/flashnews/startups-lead-claude-code-adoption-key-trends-for-crypto-project-development-in-2025\">Blockchain News</a></p>\n",
       "</blockquote>\n",
       "<p><strong>YC Startups as Case Studies:</strong><br />\n",
       "- <strong>HumanLayer (F24)</strong> - Built their entire platform with Claude Code<br />\n",
       "- <strong>Ambral (W25)</strong> - Scaling AI-powered account management with sub-agent workflows<br />\n",
       "- <strong>Vulcan Technologies (S25)</strong> - Using Claude Code for regulatory complexity</p>\n",
       "<p>As noted by <a href=\"https://www.claude.com/blog/building-companies-with-claude-code\">Anthropic's blog</a>: <em>\"Founders can now ship products directly from the terminal, compressing development cycles from weeks to hours.\"</em></p>\n",
       "<hr />\n",
       "<h3>2. ✅ <strong>Educational Use Favors Claude.ai - VALIDATED</strong></h3>\n",
       "<p>The chart showed Claude.ai leading significantly in:<br />\n",
       "- Tutorial or Learning: <strong>12.2%</strong> (vs 0.7% for Claude Code)<br />\n",
       "- Academic Research: <strong>11.9%</strong> (vs 2.2%)<br />\n",
       "- Coursework: <strong>4.9%</strong> (vs 0.4%)</p>\n",
       "<p>This aligns perfectly with Anthropic's dedicated education initiatives:</p>\n",
       "<blockquote>\n",
       "<p><strong>Anthropic launched \"Claude for Education\"</strong> with features like <strong>\"Learning Mode\"</strong> that uses Socratic questioning rather than giving direct answers. - <a href=\"https://venturebeat.com/ai/anthropic-flips-the-script-on-ai-in-education-claude-learning-mode-makes-students-do-the-thinking/\">VentureBeat</a></p>\n",
       "</blockquote>\n",
       "<p><strong>Key Statistics from Anthropic's Education Report:</strong><br />\n",
       "- <strong>39.3%</strong> of student conversations involve creating and improving educational content<br />\n",
       "- <strong>33.5%</strong> involve getting technical explanations for academic assignments<br />\n",
       "- <strong>57%</strong> of higher ed instructor chats involved developing curricula<br />\n",
       "- <strong>13%</strong> were conducting academic research</p>\n",
       "<p>Early adopters include <strong>Northeastern University</strong> (50,000+ students across 13 campuses), <strong>London School of Economics</strong>, and <strong>Champlain College</strong>. - <a href=\"https://www.anthropic.com/news/anthropic-education-report-how-educators-use-claude\">Anthropic Education Report</a></p>\n",
       "<hr />\n",
       "<h3>3. ✅ <strong>Coding/Development Dominance - VALIDATED</strong></h3>\n",
       "<p>The chart showed Personal Projects and Startup Work (both development-heavy) as top uses for Claude Code (<strong>36%</strong> and <strong>32.9%</strong>).</p>\n",
       "<p>Anthropic's own research confirms:</p>\n",
       "<blockquote>\n",
       "<p><strong>\"About 44% of API traffic involved coding, compared with 36% on Claude.ai.\"</strong> - <a href=\"https://www.anthropic.com/research/anthropic-economic-index-september-2025-report\">Anthropic Economic Index</a></p>\n",
       "<p><strong>\"Software development remains Claude's most common use case, making up more than a third of activity globally.\"</strong> - <a href=\"https://www.eweek.com/news/anthropic-economic-index-claude-ai-usage/\">eWeek</a></p>\n",
       "</blockquote>\n",
       "<hr />\n",
       "<h3>4. ✅ <strong>Product Differentiation (Claude Code vs Claude.ai) - VALIDATED</strong></h3>\n",
       "<p>The chart's overall pattern showing Claude Code for development and Claude.ai for knowledge work is confirmed by Fortune:</p>\n",
       "<blockquote>\n",
       "<p><strong>\"ChatGPT is emerging increasingly as a personal or exploratory tool... while Claude is a more work-focused productivity tool, used heavily for coding, research, and business automation.\"</strong> - <a href=\"https://fortune.com/2025/09/15/openai-chatgpt-claude-anthropic-work-personal-use-new-data/\">Fortune</a></p>\n",
       "</blockquote>\n",
       "<p><strong>Automation patterns differ by platform:</strong><br />\n",
       "- <strong>77% of API tasks</strong> are automated (full task delegation)<br />\n",
       "- <strong>~50% of Claude.ai tasks</strong> are automated (more collaborative)</p>\n",
       "<hr />\n",
       "<h3>5. 📊 <strong>Broader AI Coding Assistant Context</strong></h3>\n",
       "<p>The chart's findings fit into the larger industry trend:</p>\n",
       "<table>\n",
       "<thead>\n",
       "<tr>\n",
       "<th>Metric</th>\n",
       "<th>2025 Data</th>\n",
       "</tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td>AI-generated/assisted code</td>\n",
       "<td><strong>41%</strong> of all code globally</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td>Developers using AI coding assistants</td>\n",
       "<td><strong>82%</strong> daily or weekly</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td>Market size projection</td>\n",
       "<td><strong>$30.1 billion by 2032</strong></td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td>Google's AI-assisted code</td>\n",
       "<td><strong>25%</strong></td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<p>Source: <a href=\"https://www.secondtalent.com/resources/ai-coding-assistant-statistics/\">AI Coding Assistant Statistics</a></p>\n",
       "<hr />\n",
       "<h3>6. ⚠️ <strong>Enterprise Work Balance - PARTIALLY EXPLAINED</strong></h3>\n",
       "<p>The chart showed Enterprise Work relatively balanced (Claude.ai: 25.9%, Claude Code: 23.8%). This makes sense given:</p>\n",
       "<ul>\n",
       "<li><strong>70-75% of Anthropic's revenue</strong> comes from enterprise API consumption</li>\n",
       "<li>Enterprises use both products depending on use case</li>\n",
       "<li>Security features like <strong>FedRAMP High certification</strong> drive enterprise adoption of both platforms</li>\n",
       "</ul>\n",
       "<hr />\n",
       "<h2>Summary</h2>\n",
       "<p>The chart's findings are <strong>strongly validated</strong> by recent data:</p>\n",
       "<table>\n",
       "<thead>\n",
       "<tr>\n",
       "<th>Chart Finding</th>\n",
       "<th>Validation Status</th>\n",
       "</tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr>\n",
       "<td>Startups prefer Claude Code (32.9% vs 13.1%)</td>\n",
       "<td>✅ Confirmed by Anthropic &amp; YC case studies</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td>Education favors Claude.ai (12.2% vs 0.7%)</td>\n",
       "<td>✅ Confirmed by Anthropic Education Report</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td>Personal projects lead both platforms</td>\n",
       "<td>✅ Confirmed by usage statistics</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td>Claude Code = development tool</td>\n",
       "<td>✅ Confirmed (44% API traffic is coding)</td>\n",
       "</tr>\n",
       "<tr>\n",
       "<td>Claude.ai = knowledge/learning tool</td>\n",
       "<td>✅ Confirmed by education initiatives</td>\n",
       "</tr>\n",
       "</tbody>\n",
       "</table>\n",
       "<hr />\n",
       "<h2>Sources</h2>\n",
       "<ul>\n",
       "<li><a href=\"https://www.claude.com/blog/building-companies-with-claude-code\">How Three YC Startups Built Their Companies with Claude Code</a></li>\n",
       "<li><a href=\"https://www.anthropic.com/startups\">Anthropic Startups Program</a></li>\n",
       "<li><a href=\"https://blockchain.news/flashnews/startups-lead-claude-code-adoption-key-trends-for-crypto-project-development-in-2025\">Startups Lead Claude Code Adoption - Blockchain News</a></li>\n",
       "<li><a href=\"https://www.anthropic.com/news/anthropic-education-report-how-educators-use-claude\">Anthropic Education Report: How Educators Use Claude</a></li>\n",
       "<li><a href=\"https://www.anthropic.com/news/anthropic-education-report-how-university-students-use-claude\">How University Students Use Claude</a></li>\n",
       "<li><a href=\"https://www.anthropic.com/news/introducing-claude-for-education\">Introducing Claude for Education</a></li>\n",
       "<li><a href=\"https://www.eweek.com/news/anthropic-economic-index-claude-ai-usage/\">Anthropic's AI Usage Study: Coding Still Dominates - eWeek</a></li>\n",
       "<li><a href=\"https://www.anthropic.com/research/anthropic-economic-index-september-2025-report\">Anthropic Economic Index September 2025 Report</a></li>\n",
       "<li><a href=\"https://fortune.com/2025/09/15/openai-chatgpt-claude-anthropic-work-personal-use-new-data/\">New Studies Show What People Really Use ChatGPT and Claude For - Fortune</a></li>\n",
       "<li><a href=\"https://backlinko.com/claude-users\">Claude Statistics 2025 - Backlinko</a></li>\n",
       "<li><a href=\"https://www.secondtalent.com/resources/ai-coding-assistant-statistics/\">AI Coding Assistant Statistics &amp; Trends 2025</a></li>\n",
       "<li><a href=\"https://venturebeat.com/ai/anthropic-flips-the-script-on-ai-in-education-claude-learning-mode-makes-students-do-the-thinking/\">Anthropic Flips the Script on AI in Education - VentureBeat</a></li>\n",
       "<li><a href=\"https://keywordseverywhere.com/blog/anthropic-claude-stats/\">55 Latest Anthropic Claude Stats - Keywords Everywhere</a></li>\n",
       "</ul></div></div><div class=\"msg-block result\"><div class=\"msg-label\">✅ Complete</div><div class=\"stats-bar\"><span class=\"stat-item\"><span class=\"stat-label\">Turns:</span> 5</span> <span class=\"stat-item\"><span class=\"stat-label\">Tokens:</span> 2,152</span> <span class=\"stat-item\"><span class=\"stat-label\">Cost:</span> $0.76</span> <span class=\"stat-item\"><span class=\"stat-label\">Duration:</span> 73.0s</span></div></div>\n",
       "        </div>\n",
       "    </div>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "visualize_conversation(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38256581",
   "metadata": {},
   "source": [
    "## Building for Production\n",
    "\n",
    "Jupyter notebooks are great for learning, but production systems need reusable modules. We've packaged the research agent into `research_agent/agent.py` with a clean interface:\n",
    "\n",
    "### Core functions:\n",
    "\n",
    "- `print_activity()` - Shows what the agent is doing in real-time (imported from shared utilities)\n",
    "- `get_activity_text()` - Extract activity text for custom handlers, such as logging or monitoring\n",
    "- `send_query()` - Main entry point for research queries with built-in activity display\n",
    "\n",
    "### Built-in best practices:\n",
    "\n",
    "The module includes the `RESEARCH_SYSTEM_PROMPT` which ensures:\n",
    "- Source URLs are always included as citations\n",
    "- Citations are formatted as markdown links for clean rendering\n",
    "- A \"Sources:\" section groups all references\n",
    "\n",
    "### Display control:\n",
    "\n",
    "The `send_query()` function has a `display_result` parameter (default: `True`):\n",
    "- `display_result=True` - Renders a styled HTML card in Jupyter notebooks\n",
    "- `display_result=False` - Returns only the text result for programmatic use\n",
    "\n",
    "This agent can now be used in any Python script!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e220b5c7-463b-4171-b687-b1ec974958de",
   "metadata": {},
   "source": [
    "For independent questions where conversation context doesn't matter.\n",
    "\n",
    "The module automatically handles:\n",
    "- Activity display during execution\n",
    "- Context reset for new conversations\n",
    "- Styled HTML rendering of the final response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3c2ca449-7a36-4b67-af47-fdb68fb3e36b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "🤖 Using: WebSearch()\n",
      "✓ Tool completed\n",
      "🤖 Thinking...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "<style>\n",
       ".pretty-card {\n",
       "    font-family: ui-sans-serif, system-ui;\n",
       "    border: 2px solid transparent;\n",
       "    border-radius: 14px;\n",
       "    padding: 14px 16px;\n",
       "    margin: 10px 0;\n",
       "    background: linear-gradient(#fff, #fff) padding-box,\n",
       "                linear-gradient(135deg, #3b82f6, #9333ea) border-box;\n",
       "    color: #111;\n",
       "    box-shadow: 0 4px 12px rgba(0,0,0,.08);\n",
       "}\n",
       ".pretty-title {\n",
       "    font-weight: 700;\n",
       "    margin-bottom: 8px;\n",
       "    font-size: 14px;\n",
       "    color: #111;\n",
       "}\n",
       ".pretty-card pre,\n",
       ".pretty-card code {\n",
       "    background: #f3f4f6;\n",
       "    color: #111;\n",
       "    padding: 8px;\n",
       "    border-radius: 8px;\n",
       "    display: block;\n",
       "    overflow-x: auto;\n",
       "    font-size: 13px;\n",
       "    white-space: pre-wrap;\n",
       "}\n",
       ".pretty-card img {\n",
       "    max-width: 100%;\n",
       "    height: auto;\n",
       "    border-radius: 8px;\n",
       "}\n",
       "/* Tables: both pandas (.pretty-table) and markdown-rendered */\n",
       ".pretty-card table {\n",
       "    border-collapse: collapse;\n",
       "    width: 100%;\n",
       "    font-size: 13px;\n",
       "    color: #111;\n",
       "    margin: 0.5em 0;\n",
       "}\n",
       ".pretty-card th,\n",
       ".pretty-card td {\n",
       "    border: 1px solid #e5e7eb;\n",
       "    padding: 6px 8px;\n",
       "    text-align: left;\n",
       "}\n",
       ".pretty-card th {\n",
       "    background: #f9fafb;\n",
       "    font-weight: 600;\n",
       "}\n",
       "/* Markdown headings */\n",
       ".pretty-card h1, .pretty-card h2, .pretty-card h3, .pretty-card h4 {\n",
       "    margin: 0.5em 0 0.3em 0;\n",
       "    color: #111;\n",
       "}\n",
       ".pretty-card h1 { font-size: 1.4em; }\n",
       ".pretty-card h2 { font-size: 1.2em; }\n",
       ".pretty-card h3 { font-size: 1.1em; }\n",
       "/* Markdown lists and paragraphs */\n",
       ".pretty-card ul, .pretty-card ol {\n",
       "    margin: 0.5em 0;\n",
       "    padding-left: 1.5em;\n",
       "}\n",
       ".pretty-card p {\n",
       "    margin: 0.5em 0;\n",
       "}\n",
       ".pretty-card hr {\n",
       "    border: none;\n",
       "    border-top: 1px solid #e5e7eb;\n",
       "    margin: 1em 0;\n",
       "}\n",
       "</style>\n",
       "<div class=\"pretty-card\"><div class=\"pretty-title\">Agent Response</div><h2>Claude Code SDK</h2>\n",
       "<p>The <strong>Claude Code SDK</strong> (now renamed to <strong>Claude Agent SDK</strong>) is a toolkit from Anthropic that allows developers to build AI agents using the same infrastructure that powers Claude Code.</p>\n",
       "<p><strong>Key capabilities:</strong><br />\n",
       "- <strong>Context management</strong> - Automatic compaction to prevent running out of context<br />\n",
       "- <strong>Rich tool ecosystem</strong> - File operations, code execution, web search, MCP extensibility<br />\n",
       "- <strong>Fine-grained permissions</strong> - Control over agent capabilities<br />\n",
       "- <strong>Production features</strong> - Error handling, session management, monitoring</p>\n",
       "<p><strong>Available in:</strong><br />\n",
       "- TypeScript (<code>@anthropic-ai/claude-code</code>)<br />\n",
       "- Python (<code>pip install claude-code-sdk</code>)<br />\n",
       "- Command line</p>\n",
       "<p>It enables building agents for coding automation, customer support, personal assistants, and more—all using the same core systems that power Claude Code.</p>\n",
       "<hr />\n",
       "<p><strong>Sources:</strong><br />\n",
       "- <a href=\"https://docs.anthropic.com/en/docs/claude-code/sdk\">Agent SDK overview - Claude Docs</a><br />\n",
       "- <a href=\"https://www.anthropic.com/engineering/building-agents-with-the-claude-agent-sdk\">Building agents with the Claude Agent SDK</a><br />\n",
       "- <a href=\"https://www.infoq.com/news/2025/06/claude-code-sdk/\">Anthropic Releases Claude Code SDK - InfoQ</a></p></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from research_agent.agent import send_query\n",
    "\n",
    "# The module handles activity display, context reset, and result visualization internally\n",
    "result = await send_query(\"What is the Claude Code SDK? Only do one websearch and be concise\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "466155ec-9f54-49d4-83cb-00032b077147",
   "metadata": {},
   "source": [
    "Now we test out a multi-turn conversation that reuses the same conversation.\n",
    "\n",
    "Multi-turn conversations work seamlessly—just pass `continue_conversation=True`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "38ba1eda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "🤖 Using: WebSearch()\n",
      "✓ Tool completed\n",
      "🤖 Thinking...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "<style>\n",
       ".pretty-card {\n",
       "    font-family: ui-sans-serif, system-ui;\n",
       "    border: 2px solid transparent;\n",
       "    border-radius: 14px;\n",
       "    padding: 14px 16px;\n",
       "    margin: 10px 0;\n",
       "    background: linear-gradient(#fff, #fff) padding-box,\n",
       "                linear-gradient(135deg, #3b82f6, #9333ea) border-box;\n",
       "    color: #111;\n",
       "    box-shadow: 0 4px 12px rgba(0,0,0,.08);\n",
       "}\n",
       ".pretty-title {\n",
       "    font-weight: 700;\n",
       "    margin-bottom: 8px;\n",
       "    font-size: 14px;\n",
       "    color: #111;\n",
       "}\n",
       ".pretty-card pre,\n",
       ".pretty-card code {\n",
       "    background: #f3f4f6;\n",
       "    color: #111;\n",
       "    padding: 8px;\n",
       "    border-radius: 8px;\n",
       "    display: block;\n",
       "    overflow-x: auto;\n",
       "    font-size: 13px;\n",
       "    white-space: pre-wrap;\n",
       "}\n",
       ".pretty-card img {\n",
       "    max-width: 100%;\n",
       "    height: auto;\n",
       "    border-radius: 8px;\n",
       "}\n",
       "/* Tables: both pandas (.pretty-table) and markdown-rendered */\n",
       ".pretty-card table {\n",
       "    border-collapse: collapse;\n",
       "    width: 100%;\n",
       "    font-size: 13px;\n",
       "    color: #111;\n",
       "    margin: 0.5em 0;\n",
       "}\n",
       ".pretty-card th,\n",
       ".pretty-card td {\n",
       "    border: 1px solid #e5e7eb;\n",
       "    padding: 6px 8px;\n",
       "    text-align: left;\n",
       "}\n",
       ".pretty-card th {\n",
       "    background: #f9fafb;\n",
       "    font-weight: 600;\n",
       "}\n",
       "/* Markdown headings */\n",
       ".pretty-card h1, .pretty-card h2, .pretty-card h3, .pretty-card h4 {\n",
       "    margin: 0.5em 0 0.3em 0;\n",
       "    color: #111;\n",
       "}\n",
       ".pretty-card h1 { font-size: 1.4em; }\n",
       ".pretty-card h2 { font-size: 1.2em; }\n",
       ".pretty-card h3 { font-size: 1.1em; }\n",
       "/* Markdown lists and paragraphs */\n",
       ".pretty-card ul, .pretty-card ol {\n",
       "    margin: 0.5em 0;\n",
       "    padding-left: 1.5em;\n",
       "}\n",
       ".pretty-card p {\n",
       "    margin: 0.5em 0;\n",
       "}\n",
       ".pretty-card hr {\n",
       "    border: none;\n",
       "    border-top: 1px solid #e5e7eb;\n",
       "    margin: 1em 0;\n",
       "}\n",
       "</style>\n",
       "<div class=\"pretty-card\"><div class=\"pretty-title\">Agent Response</div><p><strong>Anthropic</strong> is an American AI safety company founded in 2021 by former OpenAI executives, including siblings Dario and Daniela Amodei. The company builds Claude, a family of large language models, with a focus on creating reliable, interpretable, and safe AI systems. Anthropic is valued at approximately $183 billion and has received major investments from Amazon, Google, Microsoft, and NVIDIA. Its Claude Code product recently reached $1 billion in run-rate revenue.</p>\n",
       "<p>Sources:<br />\n",
       "- <a href=\"https://en.wikipedia.org/wiki/Anthropic\">Anthropic - Wikipedia</a><br />\n",
       "- <a href=\"https://www.anthropic.com/\">Anthropic Homepage</a><br />\n",
       "- <a href=\"https://www.voiceflow.com/blog/anthropic-ai\">What's Anthropic AI? - Voiceflow</a></p></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "result1 = await send_query(\"What is Anthropic? Only do one websearch and be concise\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "36931a9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "🤖 Thinking...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "<style>\n",
       ".pretty-card {\n",
       "    font-family: ui-sans-serif, system-ui;\n",
       "    border: 2px solid transparent;\n",
       "    border-radius: 14px;\n",
       "    padding: 14px 16px;\n",
       "    margin: 10px 0;\n",
       "    background: linear-gradient(#fff, #fff) padding-box,\n",
       "                linear-gradient(135deg, #3b82f6, #9333ea) border-box;\n",
       "    color: #111;\n",
       "    box-shadow: 0 4px 12px rgba(0,0,0,.08);\n",
       "}\n",
       ".pretty-title {\n",
       "    font-weight: 700;\n",
       "    margin-bottom: 8px;\n",
       "    font-size: 14px;\n",
       "    color: #111;\n",
       "}\n",
       ".pretty-card pre,\n",
       ".pretty-card code {\n",
       "    background: #f3f4f6;\n",
       "    color: #111;\n",
       "    padding: 8px;\n",
       "    border-radius: 8px;\n",
       "    display: block;\n",
       "    overflow-x: auto;\n",
       "    font-size: 13px;\n",
       "    white-space: pre-wrap;\n",
       "}\n",
       ".pretty-card img {\n",
       "    max-width: 100%;\n",
       "    height: auto;\n",
       "    border-radius: 8px;\n",
       "}\n",
       "/* Tables: both pandas (.pretty-table) and markdown-rendered */\n",
       ".pretty-card table {\n",
       "    border-collapse: collapse;\n",
       "    width: 100%;\n",
       "    font-size: 13px;\n",
       "    color: #111;\n",
       "    margin: 0.5em 0;\n",
       "}\n",
       ".pretty-card th,\n",
       ".pretty-card td {\n",
       "    border: 1px solid #e5e7eb;\n",
       "    padding: 6px 8px;\n",
       "    text-align: left;\n",
       "}\n",
       ".pretty-card th {\n",
       "    background: #f9fafb;\n",
       "    font-weight: 600;\n",
       "}\n",
       "/* Markdown headings */\n",
       ".pretty-card h1, .pretty-card h2, .pretty-card h3, .pretty-card h4 {\n",
       "    margin: 0.5em 0 0.3em 0;\n",
       "    color: #111;\n",
       "}\n",
       ".pretty-card h1 { font-size: 1.4em; }\n",
       ".pretty-card h2 { font-size: 1.2em; }\n",
       ".pretty-card h3 { font-size: 1.1em; }\n",
       "/* Markdown lists and paragraphs */\n",
       ".pretty-card ul, .pretty-card ol {\n",
       "    margin: 0.5em 0;\n",
       "    padding-left: 1.5em;\n",
       "}\n",
       ".pretty-card p {\n",
       "    margin: 0.5em 0;\n",
       "}\n",
       ".pretty-card hr {\n",
       "    border: none;\n",
       "    border-top: 1px solid #e5e7eb;\n",
       "    margin: 1em 0;\n",
       "}\n",
       "</style>\n",
       "<div class=\"pretty-card\"><div class=\"pretty-title\">Agent Response</div><p>Based on my previous search results, here are Anthropic's main products:</p>\n",
       "<ol>\n",
       "<li>\n",
       "<p><strong>Claude</strong> - Their flagship AI assistant and family of large language models, which incorporates \"Constitutional AI\" for safety. Named after mathematician Claude Shannon.</p>\n",
       "</li>\n",
       "<li>\n",
       "<p><strong>Claude Code</strong> - A developer tool that recently reached $1 billion in run-rate revenue just six months after public launch.</p>\n",
       "</li>\n",
       "<li>\n",
       "<p><strong>Bun</strong> - A JavaScript runtime that Anthropic recently acquired to accelerate Claude Code's capabilities.</p>\n",
       "</li>\n",
       "</ol>\n",
       "<p>Anthropic offers Claude through their website, API access for developers, and enterprise solutions.</p>\n",
       "<p>Sources:<br />\n",
       "- <a href=\"https://www.anthropic.com/\">Anthropic Homepage</a><br />\n",
       "- <a href=\"https://www.anthropic.com/news/anthropic-acquires-bun-as-claude-code-reaches-usd1b-milestone\">Anthropic acquires Bun as Claude Code reaches $1B milestone</a></p></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Continue the conversation to dig deeper by setting continue_conversation=True\n",
    "result2 = await send_query(\n",
    "    \"What are some of their products?\",\n",
    "    continue_conversation=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5344587c",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "### What You Built\n",
    "\n",
    "In this cookbook, you built three progressively sophisticated research agents:\n",
    "\n",
    "- Stateless research agent - One-line queries for independent research tasks\n",
    "- Stateful agent with memory - Multi-turn investigations that build on previous findings\n",
    "- Production module - Reusable research functions for integration into applications\n",
    "\n",
    "### Key Takeaways\n",
    "\n",
    "**When to use stateless queries (query()):**\n",
    "\n",
    "- Independent research questions\n",
    "- Parallel processing of unrelated tasks\n",
    "- Scenarios requiring fresh context each time\n",
    "\n",
    "**When to use stateful agents (ClaudeSDKClient):**\n",
    "\n",
    "- Multi-turn investigations building on previous findings\n",
    "- Iterative refinement of research\n",
    "- Complex analysis requiring sustained context\n",
    "\n",
    "Research agents excel when information isn't self-contained and the optimal workflow emerges during exploration—competitive analysis, technical troubleshooting, literature reviews, and investigative journalism all fit this pattern.\n",
    "\n",
    "### Next Steps\n",
    "\n",
    "This foundation in autonomous research prepares you for enterprise-grade multi-agent systems. In the next notebook, you'll learn to:\n",
    "\n",
    "Orchestrate specialized subagents under a coordinating agent\n",
    "Implement governance through hooks and custom commands\n",
    "Adapt output styles for different stakeholders (executives vs. technical teams)\n",
    "\n",
    "Next: [01_The_chief_of_staff_agent.ipynb](01_The_chief_of_staff_agent.ipynb) - From single agents to multi-agent orchestration."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cc-sdk-tutorial",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}